package cn.copera.dyy.service;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.copera.dyy.common.service.BaseService;
import cn.copera.dyy.dao.ExchangeRecordDao;
import cn.copera.dyy.dao.ShopDao;
import cn.copera.dyy.entity.ExchangeRecord;
import cn.copera.dyy.entity.Shop;

/**
 * 店面兑换记录Service
 * 
 */
@Service
@Transactional(readOnly = true)
public class ExchangeRecordService extends BaseService {

	@Autowired
	private ExchangeRecordDao exchangeRecordDao ;
	@Autowired
	private ShopDao shopDao;

	@Transactional(readOnly = false)
	public void save(ExchangeRecord exchangeRecord) {
		exchangeRecordDao.clear();
		exchangeRecordDao.save(exchangeRecord);
	}
	
	public Shop findShopById(String id){
		return shopDao.get(id);
	}
	/**
	 * 今天的兑换
	 * @param account
	 * @return
	 */
	public List<ExchangeRecord> findExchangeToday(String shopId){
		DateFormat format = new SimpleDateFormat("yyyy-MM-dd");  
		String dateStr = format.format(new Date());
		String sql = "shop.id = '"+shopId+"' and exchangeDate >= '"+dateStr+" 00:00:00' and exchangeDate <= '"+dateStr+" 23:59:59'";
		return exchangeRecordDao.findEntityList(new ExchangeRecord(), "exchangeDate desc", sql);
	}
	/**
     * 最近3天的兑换
     * @param account
     * @return
     */
    public Map<String, Object> findExchangeOld(String shopId){
    	Map<String, Object> map = new HashMap<String,Object>();
    	Date date = new Date();
    	DateFormat format = new SimpleDateFormat("yyyy-MM-dd");  
		String dateSta3 = format.format(new Date(date.getTime() - 3 * 24 * 60 * 60 * 1000));
		String dateSta2 = format.format(new Date(date.getTime() - 2 * 24 * 60 * 60 * 1000));
		String dateSta1 = format.format(new Date(date.getTime() - 1 * 24 * 60 * 60 * 1000));
		String dateEnd = format.format(new Date());
		List<ExchangeRecord> list3 = exchangeRecordDao.findEntityList(new ExchangeRecord(), "exchangeDate desc", "shop.id = '"+shopId+"' and exchangeDate >= '"+dateSta3+" 00:00:00' and exchangeDate < '"+dateSta2+" 00:00:00'");
		List<ExchangeRecord> list2 = exchangeRecordDao.findEntityList(new ExchangeRecord(), "exchangeDate desc", "shop.id = '"+shopId+"' and exchangeDate >= '"+dateSta2+" 00:00:00' and exchangeDate < '"+dateSta1+" 00:00:00'");
		List<ExchangeRecord> list1 = exchangeRecordDao.findEntityList(new ExchangeRecord(), "exchangeDate desc", "shop.id = '"+shopId+"' and exchangeDate >= '"+dateSta1+" 00:00:00' and exchangeDate < '"+dateEnd+" 00:00:00'");
		map.put("list3", list3);
		map.put("list2", list2);
		map.put("list1", list1);
		map.put("countOld", countExchangeOld(shopId));
		return map;
	}
    /**
     * 统计今天兑换
     * @param shopId
     * @return
     */
    public Integer countExchangeToday(String shopId){
    	
    	DateFormat format = new SimpleDateFormat("yyyy-MM-dd");  
		String dateStr = format.format(new Date());
		return exchangeRecordDao.getEntityCount(new ExchangeRecord(), "shop.id = '"+shopId+"' and exchangeDate >= '"+dateStr+" 00:00:00' and exchangeDate <= '"+dateStr+" 23:59:59'");
    }
    /**
     * 统计最近3天兑换
     * @param shopId
     * @return
     */
    public Integer countExchangeOld(String shopId){
    	
    	DateFormat format = new SimpleDateFormat("yyyy-MM-dd");  
		String dateStr = format.format(new Date());
		Date date = new Date();
		String dateSta = format.format(new Date(date.getTime() - 3 * 24 * 60 * 60 * 1000));
		return exchangeRecordDao.getEntityCount(new ExchangeRecord(), "shop.id = '"+shopId+"' and exchangeDate >= '"+dateSta+" 00:00:00' and exchangeDate <= '"+dateStr+" 00:00:00'");
    }
}

